import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttermarketingplus/routers/jump_receive.dart';
import 'package:fluttermarketingplus/utils/auto_layout.dart';
import 'package:fluttermarketingplus/widgets/appbar_widget.dart';
import 'package:fluttermarketingplus/widgets/setting.widget.dart';
import 'package:oktoast/oktoast.dart';

//添加新用户页面
class AddConsumerPage extends StatefulWidget {
  //从上个页面传递过来的参数
  final String consumerData;

  const AddConsumerPage({Key key, this.consumerData}) : super(key: key);

  @override
  _AddConsumerPageState createState() => _AddConsumerPageState();
}

class _AddConsumerPageState extends State<AddConsumerPage> {
  //用户数据
  String _consumerData;

  //姓名控制器
  TextEditingController _nameController;

  //性别控制器
  String _genderController;

  //性别选项列表
  List<String> _genderOptions;

  //手机号码控制器
  TextEditingController _phoneController;

  //地址控制器
  TextEditingController _addressController;

  //衣服控制器
  String _clothesController;

  List<String> _clothesOptions;

  //衣服品质控制器
  TextEditingController _qualityController;

  //品牌控制器
  TextEditingController _brandController;

  @override
  void initState() {
    _consumerData = JumpReceive().receive(widget.consumerData);
    _nameController = TextEditingController();
    _genderController = '请选择性别';
    _genderOptions = ['男', '女'];
    _phoneController = TextEditingController();
    _addressController = TextEditingController();
    _addressController = TextEditingController();
    _clothesController = '请选择衣物品质';
    _clothesOptions = ['普通衣物', '名贵衣物'];
    _qualityController = TextEditingController();
    _brandController = TextEditingController();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarWidget(
        title: _consumerData,
      ),
      body: SingleChildScrollView(
        child: Container(
          margin: EdgeInsets.only(
              left: ScreenUtil().setWidth(50),
              right: ScreenUtil().setWidth(50),
              top: AutoLayout.instance.pxToDp(40)),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: <Widget>[
              _rowLine('姓名', _nameController),
              _popSelect('性别', _genderController, _genderOptions),
              _rowLine('电话', _phoneController),
              _rowLine('地址', _addressController),
              _popSelect('衣服颜色', _clothesController, _clothesOptions),
              _rowLine('衣服品质', _qualityController),
              _rowLine('品牌', _brandController),
              _btnWidget()
            ],
          ),
        ),
      ),
    );
  }

  Widget _popSelect(String title, String headController, List<String> options) {
    return Container(
      child: Row(
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(right: ScreenUtil().setWidth(40)),
            child: Text(
              '${title}:',
              style: TextStyle(
                  color: Colors.black, fontSize: ScreenUtil().setSp(35)),
            ),
          ),
          Expanded(child: _singleSelectWidget(headController, options))
        ],
      ),
    );
  }

  //单选悬浮窗
  Widget _singleSelectWidget(String headController, List<String> options) {
    return DiscreteSetting(
      head: headController,
      options: options,
      onSelected: (String value) async {
        for (int i = 0; i < options.length; i++) {
          if (value == '男') {
            showToast('选择为男');
            setState(() {
              _genderController = '男';
            });
          } else if (value == '女') {
            showToast('选择为女');
            setState(() {
              _genderController = '女';
            });
          } else if (value == '普通衣物') {
            setState(() {
              _clothesController = '普通衣物';
            });
          } else if (value == '名贵衣物') {
            setState(() {
              _clothesController = '名贵衣物';
            });
          }
        }
      },
    );
  }

  //单行样式
  Widget _rowLine(String text, TextEditingController controller) {
    return Container(
      height: AutoLayout.instance.pxToDp(80),
      margin: EdgeInsets.only(bottom: AutoLayout.instance.pxToDp(20)),
      child: Row(
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(right: ScreenUtil().setWidth(40)),
            child: Text(
              '${text}:',
              style: TextStyle(
                  color: Colors.black, fontSize: ScreenUtil().setSp(35)),
            ),
          ),
          Expanded(
              child: Container(
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(5)),
                border: Border.all(color: Colors.grey)),
            child: TextField(
              decoration: InputDecoration(
                  border: InputBorder.none,
                  contentPadding: EdgeInsets.only(
                      bottom: AutoLayout.instance.pxToDp(10),
                      left: ScreenUtil().setWidth(5))),
            ),
          ))
        ],
      ),
    );
  }

  //按钮样式
  Widget _btnWidget() {
    return Container(
      margin: EdgeInsets.only(
          left: ScreenUtil().setWidth(50),
          top: AutoLayout.instance.pxToDp(200)),
      child: FlatButton(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(6),
          ),
          color: Colors.orangeAccent,
          onPressed: () {
            showToast('保存客户信息完毕');
            Navigator.pop(context);
          },
          child: Padding(
            padding: EdgeInsets.only(
                left: ScreenUtil().setWidth(50),
                right: ScreenUtil().setWidth(50),
                top: AutoLayout.instance.pxToDp(20),
                bottom: AutoLayout.instance.pxToDp(20)),
            child: Text(
              '保存',
              style: TextStyle(
                  color: Colors.black, fontSize: ScreenUtil().setSp(35)),
            ),
          )),
    );
  }
}
